﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PhotogalleryModel
{
    public static class PagingMethods
    {
        public static ItemsPage < T> GetPage<T>(this IEnumerable< T> content,int? onPage,int currentPage,out int totalPages) where T:class
        {
            
            ItemsPage<T> result = new ItemsPage<T>();
            result.PageNumber = 1;
            totalPages = 1;
           
            if (onPage==null)
            {
                result.Page = content;
                return result;
            }
            totalPages = (int)Math.Ceiling((double)content.Count() / onPage.Value);
            if ( currentPage <= totalPages)
            {
                result.PageNumber = currentPage;

            }  
             //poulating page 
            result.Page = content.Skip(onPage.Value*(result.PageNumber - 1)).Take(onPage.Value );
            return result;
        }

    }

    public class ItemsPage<T>where T :class
    {
        public IEnumerable<T> Page { get; set; }
        public int PageNumber { get; set; }

    }
}
